# coding=utf-8
"""
Created on 2018年8月9日
"""
import unittest
import time
import json
import hashlib
import requests


class InternalTestCases(unittest.TestCase):
    
    # run before each test method
    def setUp(self):
        """
        """
        # LocalHost
        self.host = "http://127.0.0.1"
        self.secretKey = 'alive-dev-2018'

        # DEV
        # self.host = "http://54.191.225.93"
        # self.host = "http://dev.alive-story.com"
        # self.host = "https://devs.alive-story.com"
        # self.secretKey = 'alive-dev-2018'

        # PROD
        # self.host = "http://52.11.238.47"  # prod-app-server-01
        # self.host = "http://18.236.82.229"  # prod-app-server-02
        # self.host = "http://prod.alive-story.com"
        # self.host = "https://prods.alive-story.com"
        # self.secretKey = 'alive-prod-2018'

        self.port = 10000
        self.uri = '/internal/v1/'
        self.url = '%s:%s%s' % (self.host, self.port, self.uri)
        self.app_version = '6.6'
        pass

    # run after each test method
    def tearDown(self):
        pass

    def test_10000(self):
        """
        test
        """

        cmdid = 10000

        headers = {
            "AppVersion": self.app_version,
            "Timestamp": str(int(time.time() * 1000)),
            "SignType": 'V1',
            "RequestSign": '',
            'cmdId': str(cmdid)
        }

        body = {
            "common": {
                'userId': 2,
                'cmdId': cmdid,
                'release': 1,
                'platform': 2,
                'sessionId': 'dc9bdf62a5e611e8abaa02aa86e00ac4',
                'userKey': '49341723a44c11e8abaa02aa86e00ac4'
            },
            "params": {
                'param1': '111'
            }
        }

        body = json.dumps(body)
        sign_str = '''AppVersion=%s&Body=%s&SignType=V1&Timestamp=%s&secretKey=%s''' \
                   % (self.app_version, body, headers['Timestamp'], self.secretKey)

        headers['RequestSign'] = hashlib.md5(sign_str.encode()).hexdigest().upper()
        start = time.time()
        res = requests.post(self.url, data=body, headers=headers)
        end = time.time()

        try:
            print(json.dumps(json.loads(res), indent=4))
        except:
            print(res.text)

        print('...耗时%sms...' % ((end - start) * 1000))

    def test_10501(self):
        """
        """

        cmdid = 10501

        headers = {
            "AppVersion": self.app_version,
            "Timestamp": str(int(time.time() * 1000)),
            "SignType": 'V1',
            "RequestSign": '',
            'cmdId': str(cmdid)
        }

        body = {
            "common": {
                'userId': 0,
                'cmdId': cmdid,
                'release': 1,
                'platform': 2,
                'sessionId': 'dc9bdf62a5e611e8abaa02aa86e00ac4',
                'userKey': '49341723a44c11e8abaa02aa86e00ac4'
            },
            "params": {
                'content': '反馈测试',
                'contact': '1234567@qq.com',
                'imageList': ['abc/aaa.jpg', 'abc/bbb.jpg', 'abc/ccc.jpg']
            }
        }

        body = json.dumps(body)
        sign_str = '''AppVersion=%s&Body=%s&SignType=V1&Timestamp=%s&secretKey=%s''' \
                   % (self.app_version, body, headers['Timestamp'], self.secretKey)

        headers['RequestSign'] = hashlib.md5(sign_str.encode()).hexdigest().upper()
        start = time.time()
        res = requests.post(self.url, data=body, headers=headers)
        end = time.time()

        try:
            print(json.dumps(json.loads(res), indent=4))
        except:
            print(res.text)

        print('...耗时%sms...' % ((end - start) * 1000))

    def test_10701(self):
        """
        """

        cmdid = 10701

        headers = {
            "AppVersion": self.app_version,
            "Timestamp": str(int(time.time() * 1000)),
            "SignType": 'V1',
            "RequestSign": '',
            'cmdId': str(cmdid)
        }

        body = {
            "common": {
                'userId': 0,
                'cmdId': cmdid,
                'release': 1,
                'platform': 2,
                'sessionId': 'dc9bdf62a5e611e8abaa02aa86e00ac4',
                'userKey': '49341723a44c11e8abaa02aa86e00ac4'
            },
            "params": {

            }
        }

        body = json.dumps(body)
        sign_str = '''AppVersion=%s&Body=%s&SignType=V1&Timestamp=%s&secretKey=%s''' \
                   % (self.app_version, body, headers['Timestamp'], self.secretKey)

        headers['RequestSign'] = hashlib.md5(sign_str.encode()).hexdigest().upper()
        start = time.time()
        res = requests.post(self.url, data=body, headers=headers)
        end = time.time()

        try:
            print(json.dumps(json.loads(res), indent=4))
        except:
            print(res.text)

        print('...耗时%sms...' % ((end - start) * 1000))

    def test_10201(self):
        """
        """

        cmdid = 10201

        headers = {
            "AppVersion": self.app_version,
            "Timestamp": str(int(time.time() * 1000)),
            "SignType": 'V1',
            "RequestSign": '',
            'cmdId': str(cmdid)
        }

        body = {
            "common": {
                'userId': 0,
                'cmdId': cmdid,
                'release': 1,
                'platform': 2,
                'sessionId': '83c39be0b7fd11e889cf068721ef9ff4',
                'userKey': '9c6f1f688f0711e88f97068721ef9ff4'
                # 'userKey': '49341723a44c11e8abaa02aa86e00ac4'
            },
            "params": {

            }
        }

        body = json.dumps(body)
        sign_str = '''AppVersion=%s&Body=%s&SignType=V1&Timestamp=%s&secretKey=%s''' \
                   % (self.app_version, body, headers['Timestamp'], self.secretKey)

        headers['RequestSign'] = hashlib.md5(sign_str.encode()).hexdigest().upper()
        start = time.time()
        res = requests.post(self.url, data=body, headers=headers)
        end = time.time()

        try:
            print(json.dumps(json.loads(res), indent=4))
        except:
            print(res.text)

        print('...耗时%sms...' % ((end - start) * 1000))

    def test_10202(self):
        """
        """

        cmdid = 10202

        headers = {
            "AppVersion": self.app_version,
            "Timestamp": str(int(time.time() * 1000)),
            "SignType": 'V1',
            "RequestSign": '',
            'cmdId': str(cmdid)
        }

        body = {
            "common": {
                'userId': 0,
                'cmdId': cmdid,
                'release': 1,
                'platform': 2,
                'sessionId': 'dc9bdf62a5e611e8abaa02aa86e00ac4',
                'userKey': '49341723a44c11e8abaa02aa86e00ac4'
            },
            "params": {
                'pullAction': False,
                'lastArticleId': 118
            }
        }

        body = json.dumps(body)
        sign_str = '''AppVersion=%s&Body=%s&SignType=V1&Timestamp=%s&secretKey=%s''' \
                   % (self.app_version, body, headers['Timestamp'], self.secretKey)

        headers['RequestSign'] = hashlib.md5(sign_str.encode()).hexdigest().upper()
        start = time.time()
        res = requests.post(self.url, data=body, headers=headers)
        end = time.time()

        try:
            print(json.dumps(json.loads(res), indent=4))
        except:
            print(res.text)

        print('...耗时%sms...' % ((end - start) * 1000))

    def test_10901(self):
        """
        """

        cmdid = 10901

        headers = {
            "AppVersion": self.app_version,
            "Timestamp": str(int(time.time() * 1000)),
            "SignType": 'V1',
            "RequestSign": '',
            'cmdId': str(cmdid)
        }

        body = {
            "common": {
                'userId': 0,
                'cmdId': cmdid,
                'release': 1,
                'platform': 2,
                'sessionId': 'dc9bdf62a5e611e8abaa02aa86e00ac4',
                'userKey': '49341723a44c11e8abaa02aa86e00ac4'
            },
            "params": {

            }
        }

        body = json.dumps(body)
        sign_str = '''AppVersion=%s&Body=%s&SignType=V1&Timestamp=%s&secretKey=%s''' \
                   % (self.app_version, body, headers['Timestamp'], self.secretKey)

        headers['RequestSign'] = hashlib.md5(sign_str.encode()).hexdigest().upper()
        start = time.time()
        res = requests.post(self.url, data=body, headers=headers)
        end = time.time()

        try:
            print(json.dumps(json.loads(res), indent=4))
        except:
            print(res.text)

        print('...耗时%sms...' % ((end - start) * 1000))

    def test_10203(self):
        """
        """

        cmdid = 10203

        headers = {
            "AppVersion": self.app_version,
            "Timestamp": str(int(time.time() * 1000)),
            "SignType": 'V1',
            "RequestSign": '',
            'cmdId': str(cmdid)
        }

        body = {
            "common": {
                'userId': 0,
                'cmdId': cmdid,
                'release': 1,
                'platform': 2,
                'sessionId': 'dc9bdf62a5e611e8abaa02aa86e00ac4',
                'userKey': '49341723a44c11e8abaa02aa86e00ac4'
            },
            "params": {
                # 'articleIds': [61],
                'articleIds': [],
                # 'lastArticleOrderId': '0000000000700000000000000000061'
                'lastArticleOrderId': ''
            }
        }

        body = json.dumps(body)
        sign_str = '''AppVersion=%s&Body=%s&SignType=V1&Timestamp=%s&secretKey=%s''' \
                   % (self.app_version, body, headers['Timestamp'], self.secretKey)

        headers['RequestSign'] = hashlib.md5(sign_str.encode()).hexdigest().upper()
        start = time.time()
        res = requests.post(self.url, data=body, headers=headers)
        end = time.time()

        try:
            print(json.dumps(json.loads(res), indent=4))
        except:
            print(res.text)

        print('...耗时%sms...' % ((end - start) * 1000))

    def test_10204(self):
        """
        """

        cmdid = 10204

        headers = {
            "AppVersion": self.app_version,
            "Timestamp": str(int(time.time() * 1000)),
            "SignType": 'V1',
            "RequestSign": '',
            'cmdId': str(cmdid)
        }

        body = {
            "common": {
                'userId': 0,
                'cmdId': cmdid,
                'release': 1,
                'platform': 2,
                'sessionId': 'dc9bdf62a5e611e8abaa02aa86e00ac4',
                'userKey': '49341723a44c11e8abaa02aa86e00ac4'
            },
            "params": {
                'articleId': 12
            }
        }

        body = json.dumps(body)
        sign_str = '''AppVersion=%s&Body=%s&SignType=V1&Timestamp=%s&secretKey=%s''' \
                   % (self.app_version, body, headers['Timestamp'], self.secretKey)

        headers['RequestSign'] = hashlib.md5(sign_str.encode()).hexdigest().upper()
        start = time.time()
        res = requests.post(self.url, data=body, headers=headers)
        end = time.time()

        try:
            print(json.dumps(json.loads(res), indent=4))
        except:
            print(res.text)

        print('...耗时%sms...' % ((end - start) * 1000))

    def test_10702(self):
        """
        """
        cmdid = 10702

        headers = {
            "AppVersion": self.app_version,
            "Timestamp": str(int(time.time() * 1000)),
            "SignType": 'V1',
            "RequestSign": '',
            'cmdId': str(cmdid)
        }

        body = {
            "common": {
                'userId': 0,
                'cmdId': cmdid,
                'release': 1,
                'platform': 2,
                'sessionId': 'dc9bdf62a5e611e8abaa02aa86e00ac4',
                'userKey': '49341723a44c11e8abaa02aa86e00ac4'
            },
            "params": {
            }
        }

        body = json.dumps(body)
        sign_str = '''AppVersion=%s&Body=%s&SignType=V1&Timestamp=%s&secretKey=%s''' \
                   % (self.app_version, body, headers['Timestamp'], self.secretKey)

        headers['RequestSign'] = hashlib.md5(sign_str.encode()).hexdigest().upper()
        start = time.time()
        res = requests.post(self.url, data=body, headers=headers)
        end = time.time()

        try:
            print(json.dumps(json.loads(res), indent=4))
        except:
            print(res.text)

        print('...耗时%sms...' % ((end - start) * 1000))


if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(InternalTestCases('test_23001'))
    unittest.TextTestRunner(verbosity=2).run(suite)
